<?php
namespace V1;
class UserLogin extends \ResourceClass
{
    public $isActive = true;
    public $remark = '用户登录';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['account'])) {
            return 'require customer account';
        }
        if (!isset($req['uuid'])) {
            return 'require device UUID';
        }
        if (!isset($req['platform'])) {
            return 'require device platform';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }

        $account = $req['account'];
        $uuid = $req['uuid'];
        $platform = $req['platform'];
        $t = $req['t'];
        $k = $req['k'];

        $result = '';
        $user_cls = \Biz\UserHandler::login($account, $t, $k);
        if (!is_object($user_cls)) {
            if ($user_cls === \Cls\User\User::ERROR_ACCOUNT) {
                return 'user not exists';
            } else if ($user_cls === \Cls\User\User::ERROR_PASSWORD) {
                return 'wrong password';
            } else {
                return '查询失败';
            }
        }
        $result['id'] = $user_cls->getId();
        $result['account'] = $user_cls->getAccount();
        $result['enterpriseId'] = $user_cls->getEnterpriseId();
        if ($user_cls->getType() === \Cls\User\User::CUSTOMER) {
            $result['roleId'] = 0;
            $result['roleName'] = '';
            $result['channelId'] = $user_cls->getChannelId();
        } else {
            $result['roleId'] = $user_cls->getEnterpriseRole()->getId();
            $result['roleName'] = $user_cls->getEnterpriseRole()->getName();
            $result['channelId'] = 0;
        }
        $enterprise_handler = new \Biz\EnterpriseHandler();
        $enterprise_cls = $enterprise_handler->getById($user_cls->getEnterpriseId());
        if (!is_object($enterprise_cls)) {
            return '企业查询失败';
        }
        if($enterprise_cls->getMemberStatus() == \Cls\Enterprise\Enterprise::MEMBER_STATUS_ZERO){
            return '对不起，您的订货企业未审核~如有疑问请联系4000-285-927';
        }
        if($enterprise_cls->getMemberStatus() == \Cls\Enterprise\Enterprise::MEMBER_STATUS_TWO){
            return '对不起，您的订货企业已被停用~如有疑问请联系4000-285-927';
        }
        if($enterprise_cls->getPackageStartTime() > date('Y-m-d H:i:s')){
            return '对不起，您的订货企业使用时间从'.$enterprise_cls->getPackageStartTime().'开始';
        }
        if($enterprise_cls->getPackageEndTime() < date('Y-m-d H:i:s')) {
            return '对不起，您的订货企业的套餐已到期~如有疑问请联系4000-285-927';
        }
        if($enterprise_cls->getDelStatus() == \Cls\Enterprise\Enterprise::IS_DEL_ONE) {
            return '对不起，您的订货企业已被禁用~如有疑问请联系4000-285-927';
        }
        $result['type'] = $user_cls->getType();
        $result['channelName'] = $user_cls->getCompany();
        $result['tel'] = $user_cls->getTelephone();
        $result['service_hotline'] = $enterprise_cls->getServiceHotline() ? $enterprise_cls->getServiceHotline() : '4000-285-927';
        $start_page_cls = \Biz\ApplicationHandler::getStartpageImageByEnterpriseId($user_cls->getEnterpriseId());
        // 企业没有设置启动时，加载默认启动页
        if (count($start_page_cls) && isset($start_page_cls[0])) {
            $result['startPage'] = $start_page_cls[0]->getIconUrl();
        } else {
            $result['startPage'] = 'http://image.freshqiao.com/files/start-page.png';
        }
        return '';
    }
}